package com.isunland.managesystem.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.isunland.managesystem.R;
import com.isunland.managesystem.entity.ReceviceOrderLog;
import java.util.ArrayList;
import java.util.List;

public class ReceviceOrderLogAdapter<T> extends BaseAdapter {

  private List<T> objects = new ArrayList<>();

  private Context context;
  private LayoutInflater layoutInflater;
  private Callback mCallback;

  public ReceviceOrderLogAdapter(Context context, ArrayList<T> objects, Callback callback) {
    this.context = context;
    this.layoutInflater = LayoutInflater.from(context);
    this.objects = objects;
    this.mCallback = callback;
  }

  @Override public int getCount() {
    return objects.size();
  }

  @Override public T getItem(int position) {
    return objects.get(position);
  }

  @Override public long getItemId(int position) {
    return position;
  }

  @SuppressWarnings("all") @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
      convertView = layoutInflater.inflate(R.layout.adapter_receive_order_log, parent, false);
      convertView.setTag(new ViewHolder(convertView));
    }

    initializeViews(getItem(position), (ViewHolder) convertView.getTag(), position);
    return convertView;
  }

  private void initializeViews(T object, ViewHolder holder, int position) {
    ReceviceOrderLog item = (ReceviceOrderLog) object;
    holder.tvCooperativeMemberName.setText(
        context.getString(R.string.cooperativeMemberNameHolder, item.getCooperativeMemberName()));
    holder.tvText.setText(item.getText());
    holder.tvRegDate.setText(
        context.getString(R.string.receivedOrderTimeHolder, item.getRegDate()));
    holder.tvRegStaffName.setText(
        context.getString(R.string.receivedOrderPersonHolder, item.getRegStaffName()));
    if (ReceviceOrderLog.TRUE_FINISH_RECEIVE_ORDER.equals(item.getIfConfirm())) {
      //接单完成,显示红色按钮.文本,设置tag为T
      holder.tvConfirmOrder.setText(R.string.receivedOrderComplete);
      //noinspection deprecation
      holder.tvConfirmOrder.setBackgroundColor(context.getResources().getColor(R.color.home_red));
    } else {
      holder.tvConfirmOrder.setText(R.string.ConfirmReceivedOrder);
      //noinspection deprecation
      holder.tvConfirmOrder.setBackgroundColor(
          context.getResources().getColor(R.color.cornflowerblue));
    }
    holder.tvConfirmOrder.setTag(item);
  }

  protected class ViewHolder {
    @BindView(R.id.iv_logo) ImageView ivLogo;
    @BindView(R.id.tv_cooperativeMemberName) TextView tvCooperativeMemberName;
    @BindView(R.id.tv_text) TextView tvText;
    @BindView(R.id.tv_regDate) TextView tvRegDate;
    @BindView(R.id.tv_regStaffName) TextView tvRegStaffName;
    @BindView(R.id.tv_confirmOrder) TextView tvConfirmOrder;

    ViewHolder(View view) {
      ButterKnife.bind(this, view);
    }

    @OnClick({ R.id.tv_confirmOrder }) public void click(View view) {
      if (view == null) return;
      mCallback.onClickReceiveOrder(view);
    }
  }

  public interface Callback {
    /** 抢单或接单完成跳转评论 */
    void onClickReceiveOrder(View v);
  }
}
